﻿namespace SAD.DataCenter
{
    using System;
    using System.Net.Sockets;
    using System.Text;
    using SAD.Common.Models;
    using System.ServiceModel;
    using SAD.Common;
    using System.ServiceModel.Description;
    using System.Collections.Generic;
    using SAD.Common.Contracts.ShowNode;

    public class Activator
    {
        #region Instance
        /// <summary>
        /// Activator实例
        /// </summary>
        public static Activator Current
        {
            get
            {
                return m_Current;
            }
        }
        private static Activator m_Current = new Activator();
        #endregion

        private System.Threading.Thread m_testInfoSendThread = null;

        private List<ServiceHost> m_Hosts = new List<ServiceHost>();

        private Activator()
        {
            m_Hosts.Add(new ServiceHost(typeof(ShowNodeManagerService)));
            m_Hosts.Add(new ServiceHost(typeof(InfoCategoryManagerService)));
            m_Hosts.Add(new ServiceHost(typeof(InfoManagerService)));
        }

        public void Start()
        {
            foreach (var host in m_Hosts)
            {
                host.Open();
            }

            m_testInfoSendThread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(delegate(object obj)
            {
                while (true)
                {
                    ShowNode_Data showNode = null;
                    for (int i = 0; i < ShowNodeManager.Instance.OnlineShowNodes.Count; i++)
                    {
                        showNode = ShowNodeManager.Instance.OnlineShowNodes[i];
                        try
                        {
                            if (showNode.OnlineStatus == 1)
                            {
                                //long infoID = InfoManager.Instance.SendInfo(1, 0, SAD.Common.Enums.Info_ShowType.NormalText, "现有2室一厅房屋出租，有意请拨打电话：13852523636", 0, SAD.Common.Enums.Info_SourceType.SMS, "system");

                                //InfoManager.Instance.ChangeVerifyStatus(infoID, SAD.Common.Enums.Info_VerifyStatus.Normal);
                            }
                        }
                        catch (System.ServiceModel.EndpointNotFoundException ex)
                        {
                            ShowNodeManager.Instance.SetOffline(showNode.Mark);
                            Exception exx = ex;
                        }
                        catch (Exception ex)
                        {
                            Exception exx = ex;
                        }
                    }

                    System.Threading.Thread.Sleep(100);
                }
            }));
            m_testInfoSendThread.Start();
        }

        public void Stop()
        {
            m_testInfoSendThread.Abort();
            m_testInfoSendThread = null;

            foreach (var host in m_Hosts)
            {
                host.Close();
            }
        }
    }
}
